package com.rchat.pocmini.activity.creategroup;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.R;
import com.rchat.pocmini.account.PocAccount;
import com.rchat.pocmini.activity.groupsselect.GroupsSelectorActivity;
import com.rchat.pocmini.activity.recharge.ReChargeMvpActivity;
import com.rchat.pocmini.activity.view.ClearEditText;
import com.rchat.pocmini.bean2eventbus.EventBusRemoteBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;
import com.rchat.pocmini.common.utils.ActivityStackManager;
import com.rchat.pocmini.global.AppGlobal;
import com.rchat.pocmini.global.IntentKeyGlobal;
import com.rchat.pocmini.service.PocService;
import com.rchat.pocmini.service.RchatClientService;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/7/31.
 */

public class CreateGroupMvpActivity extends BaseMvpActivity<CreateGroupPresenterImpl, CreateGroupModelImpl> implements
        CreateGroupContract.View {

    @BindView(R.id.btn_back)
    ImageView mIvBack;

    @BindView(R.id.et_group_name)
    ClearEditText mEtGroupName;

    @BindView(R.id.btn_create)
    Button mBtnCreate;

    @BindView(R.id.layout_vip_guide)
    View mLayoutVipGuide;
    @BindView(R.id.btn_select_groups)
    View mBtnSelectGroups;

    @BindView(R.id.pb)
    ProgressBar mPb;

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.create_group_activity_layout;
    }

    @Override
    public void initView() {
        updateVipGuide(1);
    }

    @Override
    public void doAfterInitView() {

    }

    @OnClick({R.id.btn_create, R.id.btn_back,
            R.id.btn_vip_intent, R.id.btn_select_groups, R.id.btn_app_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_create:
                ALog.d("btn_create");
                mPresenter.createGroup(AirtalkeeAccount.getInstance().getUserId(), mEtGroupName.getText().toString().trim());
                break;
            case R.id.btn_vip_intent:
                Intent intentRecharge = new Intent(this, ReChargeMvpActivity.class);
                intentRecharge.putExtra(IntentKeyGlobal.IntentRecharge.INTENT_KEY_FROM, IntentKeyGlobal.IntentRecharge.INTENT_VALUE_CREATEGROUP);
                startActivity(intentRecharge);
                break;
            case R.id.btn_select_groups:
                Intent intent = new Intent(this, GroupsSelectorActivity.class);
                intent.putExtra(AppGlobal.KeyIntent.KEY_INTENT_UPDATE_GROUPS_BACK, false);
                startActivity(intent);
                break;
            case R.id.btn_back:
                ALog.d("btn_back");
                finish();
                break;
            case R.id.btn_app_exit:
                EventBusRemoteBean bean = new EventBusRemoteBean();
                bean.setCurrentEvent(EventBusRemoteBean.EVENT_UNBINDREMOTE);
                bean.setTarget(this.getClass(), RchatClientService.class, "onEventBusDispatchRemote");
                EventBus.getDefault().post(bean);
                ALog.dTag("appexit", "type = btn_app_exit");
                PocAccount.getPocAccountInstance(this).logout();
                //退出友盟
                stopService(new Intent(this, PocService.class));
                MobclickAgent.onProfileSignOff();
                ActivityStackManager.getStackManagerUtils().finishAllActivity();
                System.exit(0);
                break;
        }
    }

    @Override
    public void setProcessBarVisibility(int visibility) {
        mPb.setVisibility(visibility);
    }

    @Override
    public void showCreateGroupSuccessDialog() {
        showNormalDialog(getString(R.string.group_create_success));
    }

    @Override
    public void updateVipGuide(int groupsNum) {
        /**
         * @author sudf
         * @date 2018.9.1
         * 群组上限：mPresenter.GROUPSMAX
         * 1、当前群组数小于群组上限时，不显示升级会员的引导界面，且可返还
         * 2、当前群组数等于群组上限时，显示升级会员的引导界面，但是不显示群组选择，且可返回
         * 3、当前群组数大于群组上限时，显示升级会员的引导界面，显示群组选择功能，且不可返回
         */
        if (groupsNum == mPresenter.GROUPSMAX) {
            mLayoutVipGuide.setVisibility(View.VISIBLE);
            mBtnSelectGroups.setVisibility(View.GONE);
        } else if (groupsNum > mPresenter.GROUPSMAX) {
            mLayoutVipGuide.setVisibility(View.VISIBLE);
            mBtnSelectGroups.setVisibility(View.VISIBLE);
        } else {
            if (mLayoutVipGuide.getVisibility() == View.VISIBLE) {
                mLayoutVipGuide.setVisibility(View.GONE);
            }
        }

        mIvBack.setVisibility(groupsNum > mPresenter.GROUPSMAX ? View.GONE : View.VISIBLE);
    }


    /**
     * 群组创建成功
     *
     * @param content 显示的内容
     */
    private void showNormalDialog(String content) {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setMessage(content);
        normalDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //...To-do
//                Intent intent = new Intent();
//                intent.setClass(CreateGroupActivity.this,MainActivity.class);
//                startActivity(intent);
                finish();
            }
        });

        // 显示
        normalDialog.show();
    }

    @Override
    public void onBackPressed() {
        if (!mPresenter.isCanBack()) {
            return;
        }
        super.onBackPressed();
    }
}
